package com.feizi.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feizi.entity.CommentEntity;
import com.feizi.entity.Product;
import com.feizi.entity.ProductType;
import com.feizi.entity.ShopcartEntity;
import com.feizi.entity.UserEntity;
import com.feizi.service.CommentService;
import com.feizi.service.ProductService;
import com.feizi.service.ShopcartService;
@Controller
public class ProductController {
	
	@Autowired
	private CommentService commentService;
	@Autowired
	private ProductService productService;
	/**
	 * 查询
	 * @param session
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequestMapping(path="/findProduct.do",method=RequestMethod.GET)
	public String findProduct(Model model,String pname,String f_ptId) {
		int ptid=0;
		Product pro=new Product();
		pro.setF_ptId(0);
		pro.setName(pname);
		if(f_ptId != null && !"".equals(f_ptId)) {
			pro.setF_ptId(Integer.parseInt(f_ptId));
			ptid=Integer.parseInt(f_ptId);
		}
		
		
		List<ProductType> producttype=productService.findProducttype();
		List<Product> product=productService.findProduct(pro);
		List<Product> productTJ=productService.findProductTJ();
		model.addAttribute("producttype", producttype);
		model.addAttribute("product", product);
		model.addAttribute("productTJ", productTJ);
		model.addAttribute("ptid", ptid);
		return "index";
	}
	/**
	 * 查看订单详情
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(path="/getProduct.do",method=RequestMethod.GET)
	public String getProduct(Model model,int id) {
		Product product=productService.getProduct(id);
		List<CommentEntity> comment = commentService.findComment(id);
		model.addAttribute("pro", product);
		model.addAttribute("comment", comment);
		return "product";
	}
	/**
	 * 加入购物车
	 * @param session
	 * @param model
	 * @returnf_uid,f_pid,pname,pimg,price,count,totalprice,size
	 */
	/*@RequestMapping(path="/setShopcart.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> setShopcart(HttpSession session,Product product) {
		UserEntity user = (UserEntity) session.getAttribute("user");
		Map<String, Object> rslMap=new HashMap<String, Object>();
		ShopcartEntity shopcart = new ShopcartEntity();
		shopcart.setF_uid(user.getUserid());
		shopcart.setF_pid(product.getId());
		shopcart.setPname(product.getName());
		shopcart.setPimg(product.getImg());
		shopcart.setPrice(product.getPrice());
		shopcart.setCount(1);
		shopcart.setTotalprice(product.getPrice());
		shopcart.setSize(product.getSize());
		int rows = shopcartService.addShopcart(shopcart);
		rslMap.put("code",rows>0?"1":"0");
		rslMap.put("msg",rows>0? "加入购物车成功" : "加入购物车失败" );
		return rslMap;
	}*/
}
